package system

import (
	"github.com/goravel/framework/contracts/http"
	httpfacade "github.com/hulutech-web/http_result"
	"github.com/spf13/cast"
	"goravel/app/services/upload"
)

type UploadController struct {
	// Dependent services
	upload_service *upload.UploadService
}

func NewUploadController() *UploadController {
	return &UploadController{
		// Inject services
		upload_service: upload.NewUploadService(),
	}
}

// Upload 新增
// @Summary      新增
// @Description  新增
// @Tags         UploadController
// @Accept       json
// @Produce      json
// @Id UploadUpload
// @Security ApiKeyAuth
// @Param Authorization header string false "Bearer 用户令牌"
// @Param UploadData body interface{} true "数据"
// @Success 200 {string} json {}
// @Router       /api/upload [post]
func (r *UploadController) Upload(ctx http.Context) http.Response {
	file, _ := ctx.Request().File("file")
	cate_id := cast.ToUint(ctx.Request().InputInt("cate_id"))
	if att, err := r.upload_service.Upload(ctx, cate_id, file); err != nil {
		return httpfacade.NewResult(ctx).Error(500, "", err.Error())
	} else {
		return ctx.Response().Success().Json(http.Json{
			"message": "上传成功",
			"data":    att,
		})
	}
}
